<?php

namespace App\Services;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;

class Http
{
    private $client;

    public function __construct()
    {
        $this->client = new Client([
            'timeout' => 30,
            'verify' => false,
            'http_errors' => false
        ]);
    }

    /**
     * 发送GET请求
     */
    public function get(string $url, array $options = []): array
    {
        try {
            $response = $this->client->get($url, $options);
            return [
                'status' => $response->getStatusCode(),
                'body' => $response->getBody()->getContents()
            ];
        } catch (GuzzleException $e) {
            return [
                'status' => 500,
                'error' => $e->getMessage()
            ];
        }
    }

    /**
     * 发送POST请求
     */
    public function post(string $url, array $data = [], array $options = []): array
    {
        try {
            $options['form_params'] = $data;
            $response = $this->client->post($url, $options);
            return [
                'status' => $response->getStatusCode(),
                'body' => $response->getBody()->getContents()
            ];
        } catch (GuzzleException $e) {
            return [
                'status' => 500,
                'error' => $e->getMessage()
            ];
        }
    }
}
